package com.hwj.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hwj.entity.Post;
import com.hwj.entity.UserSearchHistory;
import com.hwj.mapper.PostMapper;
import com.hwj.mapper.UserInfoMapper;
import com.hwj.mapper.UserSearchHistoryMapper;
import com.hwj.service.PostService;
import com.hwj.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class PostServiceImpl implements PostService {
    @Autowired
    PostMapper postMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    UserSearchHistoryMapper userSearchHistoryMapper;


    @Override
    public PageInfo<Post> page(Post post, String postTypeName, Integer pageNum, Integer pageSize) {
        if (post == null)
            post = new Post();
        String content = post.getContent();
        String title = post.getTitle();
        if (content != null) {
            content = "%" + post.getContent() + "%";
            post.setContent(content);
        }
        if (title != null) {
            title = "%" + post.getTitle() + "%";
            post.setTitle(title);
        }
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 3;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Post> page = postMapper.page(post, postTypeName);
        return new PageInfo<>(page);
    }

    @Override
    public PageInfo<Post> pageManage(Post post, Integer pageNum, Integer pageSize) {
        if (post == null)
            post = new Post();
        String username = post.getUsername();
        String title = post.getTitle();
        String postTypeName = post.getPostTypeName();
        if (username != null) {
            username = "%" + post.getUsername() + "%";
        }
        if (title != null) {
            title = "%" + post.getTitle() + "%";

        }
        if (postTypeName != null) {
            postTypeName = "%" + post.getPostTypeName() + "%";

        }
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 3;
        }
        post.setUsername(username);
        post.setTitle(title);
        post.setPostTypeName(postTypeName);
        PageHelper.startPage(pageNum, pageSize);
        List<Post> pageManger = postMapper.pageManage(post);
        return new PageInfo<>(pageManger);
    }

    @Override
    public int insertSelective(Post record) {
        return postMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKey(Post record) {
        return postMapper.updateByPrimaryKey(record);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return postMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Post record) {
        return postMapper.insert(record);
    }

    @Override
    public List<Post> selectView() {
        List<Post> page = postMapper.page(new Post(), null);
        page = page.stream().sorted((p1, p2) -> p2.getViews().compareTo(p1.getViews())).limit(20).collect(Collectors.toList());
        return page;
    }

    @Override
    public List<Post> selectFavorites() {
        List<Post> page = postMapper.page(new Post(), null);
        page = page.stream().sorted((p1, p2) -> p2.getFavorites().compareTo(p1.getFavorites())).limit(20).collect(Collectors.toList());
        return page;
    }

    @Override
    public List<Post> selectLikes() {
        List<Post> page = postMapper.page(new Post(), null);
        page = page.stream().sorted((p1, p2) -> p2.getLikes().compareTo(p1.getLikes())).limit(20).collect(Collectors.toList());
        return page;
    }

    @Override
    public Post selectByPrimaryKey(Long id) {
        return postMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Post> selectByUserIdPosts(Long userId) {
        return postMapper.selectByUserIdPosts(userId);
    }

    @Override
    public Set selectRecommend(Long postId) {
        Post post = postMapper.selectByPrimaryKey(postId);
        String tags= post.getTag();
        HashSet<Post> posts = new HashSet<>();
        if(ObjectUtil.isNotEmpty(tags)){
            List<Post> posts1 = postMapper.pageManage(new Post());
            JSONArray tagsArr = JSONUtil.parseArray(tags);
            for(Object o:tagsArr){
                Set<Post> collect = posts1.stream().filter((b) -> b.getTag().contains(o.toString()) && !postId.equals(b.getPostId())).collect(Collectors.toSet());
                posts.addAll(collect);
            }
        }

        return posts;
    }

    @Override
    public PageInfo<Post> pageExam(Post post, Integer pageNum, Integer pageSize) {
        if (post == null)
            post = new Post();
        String username = post.getUsername();
        String title = post.getTitle();
        String postTypeName = post.getPostTypeName();
        if (username != null) {
            username = "%" + post.getUsername() + "%";
        }
        if (title != null) {
            title = "%" + post.getTitle() + "%";

        }
        if (postTypeName != null) {
            postTypeName = "%" + post.getPostTypeName() + "%";

        }
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 3;
        }
        post.setUsername(username);
        post.setTitle(title);
        post.setPostTypeName(postTypeName);
        PageHelper.startPage(pageNum, pageSize);
        List<Post> pageManger = postMapper.pageExam(post);
        return new PageInfo<>(pageManger);
    }

    @Override
    public List<Post> myStarPost(Long userId) {
        return postMapper.myStarPost(userId);
    }

    @Override
    public List<Post> myExamPost(Long userId) {
        return postMapper.myExamPost(userId);
    }

    @Override
    public List<Post> myPublishPost(Long userId) {
        return postMapper.myPublishPost(userId);
    }

    @Override
    public PageInfo<Post> search(String title,Integer pageNum,Integer pageSize,Long userId) {
        //插入搜索记录
        if(StrUtil.isNotEmpty(title)){
            UserSearchHistory userSearchHistory = new UserSearchHistory();
            userSearchHistory.setUserId(userId);
            userSearchHistory.setContent(title);
            List<UserSearchHistory> userSearchHistories = userSearchHistoryMapper.listByUserId(userId);
            Boolean flag=true;
            for (UserSearchHistory searchHistory:userSearchHistories){
                if(searchHistory.getContent().equals(title)){
                    flag=false;
                    searchHistory.setCreateTime(new Date());
                    userSearchHistoryMapper.updateByPrimaryKeySelective(searchHistory);
                    break;
                }
            }
            if(flag) {
                userSearchHistoryMapper.insertSelective(userSearchHistory);
            }
        }
        //查询关键词相关的帖子信息
        if(title==null){
            title="";
        }
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 3;
        }
        title="%"+title+"%";
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(postMapper.search(title));
    }

    @Override
    public PageInfo<Post> tag(String tag,Integer pageNum, Integer pageSize) {
        if(tag==null){
            tag="";
        }
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 3;
        }
        tag="%"+tag+"%";
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(postMapper.tag(tag));
    }

    @Override
    public List<Post> listAll() {
        return postMapper.listAll();
    }

    @Override
    public int deleteForever(Long postId) {
        return postMapper.deleteForever(postId);
    }

    @Override
    public List<Post> underPost(Long userId) {
        return postMapper.underPost(userId);
    }

    @Override
    public List<Post> myLikePost(Long userId) {
        return postMapper.myLikePost(userId);
    }

    @Override
    public List<Post> myPost(long l) {
        return postMapper.myPost(l);
    }


}
